package cn.sm1234.controller;

import cn.sm1234.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;

@Controller
@RequestMapping("/user")
public class UserController {

    // 跳转到add.html
    @RequestMapping("toAdd")
    public String toAdd(){
        return "add";
    }

    // 用户添加
    // BindingResult 用于封装验证的结果
    @RequestMapping("add")
    public String add(@Valid User user, BindingResult result){
        System.out.println("保存用户：" + user.toString());
        if (result.hasErrors()){
            return "add";
        }
        System.out.println("保存用户：" + user.toString());
        return "succ";
    }
}